package com.venue.hjt.venueclass.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Base64;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.venue.hjt.venueclass.R;
import com.venue.hjt.venueclass.bean.VenueBooKingDetaitBean;
import com.venue.hjt.venueclass.ui.FaceTrackingActivity;
import com.venue.hjt.venueclass.view.CircleImageView;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.List;

/**
 * Created by xqx on 2017/10/25.
 * 照片墙
 */

public class VenueSignFaceAdapter extends BaseAdapter {
    private Context mContext;
    private List<VenueBooKingDetaitBean.FaceErr> list;
    private int type;
    private String tid;
    public VenueSignFaceAdapter(Context mContext, List<VenueBooKingDetaitBean.FaceErr> list,int type,String tid) {
        this.mContext = mContext;
        this.list = list;
        this.type=type;
        this.tid=tid;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHander viewHander;
        if (convertView==null){
            viewHander=new ViewHander();
            convertView=View.inflate(mContext, R.layout.venue_sign_face_item,null);
            viewHander.tv_noFace=convertView.findViewById(R.id.tv_noFace);
            viewHander.iv_student_url1=convertView.findViewById(R.id.iv_student_url1);
            viewHander.iv_student_url=convertView.findViewById(R.id.iv_student_url);
            viewHander.tv_nameface=convertView.findViewById(R.id.tv_nameFace);
            convertView.setTag(viewHander);

        }else{
            viewHander= (ViewHander) convertView.getTag();
        }
        final VenueBooKingDetaitBean.FaceErr faceErr = list.get(position);
        Bitmap bitmap = decodeImg(faceErr.image);
        viewHander.iv_student_url1.setImageBitmap(bitmap);
        if (!faceErr.headimg.equals("")){
                Glide.with(mContext).load(faceErr.headimg).into(viewHander.iv_student_url);
        }else {
            viewHander.iv_student_url.setImageResource(R.mipmap.student);
        }
        viewHander.tv_nameface.setText(faceErr.name);
//        if (type==0){
//            viewHander.tv_noFace.setVisibility(View.VISIBLE);
////                viewHander.tv_sign.setVisibility(View.VISIBLE);
//                viewHander.tv_noFace.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        Intent intent=new Intent(mContext, FaceTrackingActivity.class);
//                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                        intent.putExtra("id",tid+faceErr.student_no);
//                        mContext.startActivity(intent);
//                    }
//                });
//
//        }
        return convertView;
    }
    class ViewHander{
        private CircleImageView iv_student_url1,iv_student_url;
        private TextView tv_nameface,tv_noFace;

    }
    private Bitmap getDiskBitmap(String pathString)
    {
        Bitmap bitmap = null;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathString, opts);
        opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128);
        opts.inJustDecodeBounds = false;
        try {
            bitmap = BitmapFactory.decodeFile(pathString, opts);
//            imageView.setImageBitmap(bmp);
        } catch (OutOfMemoryError err) {
        }
        return bitmap;
    }
    private  int computeInitialSampleSize(BitmapFactory.Options options,
                                          int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
                .sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
                Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
    public  int computeSampleSize(BitmapFactory.Options options,
                                  int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength,
                maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }
    /**
     * 将从Message中获取的，表示图片的字符串解析为Bitmap对象
     *
     * @param picStrInMsg
     * @return
     */
    public static Bitmap decodeImg(String picStrInMsg) {
        Bitmap bitmap = null;

        byte[] imgByte = null;
        InputStream input = null;
        try {
            imgByte = Base64.decode(picStrInMsg, Base64.DEFAULT);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            input = new ByteArrayInputStream(imgByte);
            SoftReference softRef = new SoftReference(BitmapFactory.decodeStream(input, null, options));
            bitmap = (Bitmap) softRef.get();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (imgByte != null) {
                imgByte = null;
            }

            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bitmap;
    }

}
